package com.ruicar.afs.cloud.image.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.image.condition.OcrDiscernCondition;
import com.ruicar.afs.cloud.image.config.OcrDiscernUrl;
import com.ruicar.afs.cloud.image.service.OcrDiscernService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 *  @Author: rongji.zhang
 *  @Date: 2020/8/12 11:41
 *  文件类型FileCode决定返回值和调用方法
 *  由于第三方接口链接超时，所以采用这种HttpResponse调接口调方式
 */
@Slf4j
@Service
@AllArgsConstructor
public class OcrDiscernServiceImpl implements OcrDiscernService {

    private OcrDiscernUrl ocrDiscernUrl;
    private final Integer connectTime=20000;
    /**
     * @param dto
     * @Description:获取银行卡OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: JSONObject
     */
    @Override
    public JSONObject bankCardOcrDiscern(OcrDiscernCondition dto) {
        try {
            if(dto.getFileCode().equals("bankCard")){
                HttpResponse res = HttpUtil.createPost(ocrDiscernUrl.getOcrUrl()).timeout(connectTime).contentType("application/json").body(JSONObject.toJSONString(dto)).execute();
                JSONObject invoiceCar = JSONObject.parseObject(res.body());
                //JSONObject invoiceCar= ocrDiscernApiFeign.discern(dto);
                return invoiceCar;
            }else {
                log.info("调用接口错误");
                return null;
            }
        }catch (Exception e){
            log.info("接口异常"+e);
            return null;
        }
    }

    /**
     * @param dto
     * @Description:获取机动车发票OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: JSONObject
     */
    @Override
    public JSONObject invoiceCarOcrDiscern(OcrDiscernCondition dto) {
        try {
            if(dto.getFileCode().equals("standardDocumentVehicleInvoice")){
                HttpResponse res = HttpUtil.createPost(ocrDiscernUrl.getOcrUrl()).timeout(connectTime).contentType("application/json").body(JSONObject.toJSONString(dto)).execute();
                JSONObject invoiceCar = JSONObject.parseObject(res.body());
                //JSONObject invoiceCar= ocrDiscernApiFeign.discern(dto);
                return invoiceCar;
            }else {
                log.info("调用接口错误");
                return null;
            }
        }catch (Exception e){
            log.info("接口异常"+e);
            return null;
        }
    }

    /**
     * @param dto
     * @Description:获取身份证正面OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Param:dto
     * @Return: JSONObject
     */
    @Override
    public JSONObject idCardFrontOcrDiscern(OcrDiscernCondition dto) {
        try {
            if(dto.getFileCode().equals("idCardFront")){
                HttpResponse res = HttpUtil.createPost(ocrDiscernUrl.getOcrUrl()).timeout(connectTime).contentType("application/json").body(JSONObject.toJSONString(dto)).execute();
                JSONObject invoiceCar = JSONObject.parseObject(res.body());
                //JSONObject invoiceCar= ocrDiscernApiFeign.discern(dto);
                return invoiceCar;
            }else {
                log.info("调用接口错误");
                return null;
            }
        }catch (Exception e){
            log.info("接口异常"+e);
            return null;
        }
    }

    /**
     * @param dto
     * @Description:获取身份证反面OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: JSONObject
     */
    @Override
    public JSONObject idCardBackOcrDiscern(OcrDiscernCondition dto) {
        try {
            if(dto.getFileCode().equals("idCardBack")){
                HttpResponse res = HttpUtil.createPost(ocrDiscernUrl.getOcrUrl()).timeout(connectTime).contentType("application/json").body(JSONObject.toJSONString(dto)).execute();
                JSONObject invoiceCar = JSONObject.parseObject(res.body());
                //JSONObject invoiceCar= ocrDiscernApiFeign.discern(dto);
                return invoiceCar;
            }else {
                log.info("调用接口错误");
                return null;
            }
        }catch (Exception e){
            log.info("接口异常"+e);
            return null;
        }
    }
}
